package com.gl.spring.restTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class RestTemplateTest {
	private RestTemplate restTemplate = new RestTemplate();
	
	public Object getURL() throws Exception{
		String message = null;
		//get方式
		//参数直接放在URL中 
		message = restTemplate.getForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijie&id=80", String.class ); 
		
		//参数使用MAP传递 
		Map<String ,Object> urlVariables = new HashMap<String ,Object>(); 
		urlVariables.put("name", "zhaoshijie"); 
		urlVariables.put("id", 80); 
		message = restTemplate.getForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate", String.class, urlVariables); 

		return message;
	}
	
	public Object postString() throws Exception {
		String url = "http://localhost:8080/urcwebservice30/controller/template";
		
		//参数使用MAP传递 
		Map<String ,Object> urlVariables = new HashMap<String ,Object>(); 
		urlVariables.put("name", "gaolei"); 
		urlVariables.put("id", 80); 
		String result = (String)(restTemplate.postForObject(url, urlVariables, String.class));
		//Object result = restTemplate.postForObject(url, urlVariables.toString(), String.class);
		//String result = restTemplate.postForObject(url, "1111111", String.class);
		System.out.println("****The result:" + result);
		
		return result;
	}
	
	public Object postJSON() throws Exception {
		String url = "http://localhost:8080/urcwebservice30/controller/templateMap";
		String jsonString = "{\"resourceId\":\"C7D85EA3-A444-47E6-B293-BE755F600997\",\"ciid\":\"001\"}";
		
		// 读取JSON字符串
		ObjectMapper mapper = new ObjectMapper();
		JsonNode rootNode = mapper.readTree(jsonString);
		String resourceId = rootNode.path("resourceId").asText();
		System.out.println("----postJSON--resourceId:" + resourceId);
		System.out.println("---postJSON---jsonString:" + jsonString);
		System.out.println("---postJSON---rootNode:" + rootNode);
		
		String result= restTemplate.postForObject(url, rootNode, String.class);
		System.out.println(result);
		return result;
	}
	
	///grab/publish
	public Object postList() throws Exception {
		String url = "http://localhost:8080/urcwebservice30/controller/templateList";
		List<String> lstring = new ArrayList<String>();
		lstring.add("11");
		lstring.add("22");
		System.out.println(lstring);
		
		String result= restTemplate.postForObject(url, lstring, String.class);
		System.out.println(result);
		return result;
	}
	
	public Object post1() throws Exception {
		String url = "http://localhost:8080/urcwebservice30/controller/material/{type}/{count}";
		
		//参数使用MAP传递 
		Map<String ,Object> uriVariables = new HashMap<String ,Object>(); 
		uriVariables.put("type", "777"); 
		uriVariables.put("count", "20"); 
		//Object result = restTemplate.postForObject(url, "参数", String.class, uriVariables);
		
		String result = restTemplate.postForObject(url, "1111111", String.class, uriVariables);
		System.out.println("****The result:" + result);
		//restTemplate.postForObject(url, request, responseType, uriVariables)
		return result;
	}
	
	
	public Object postIngest(String req) throws Exception {
		String url = "http://localhost:8080/urcwebservice30/controller/ingestFeedBack";
		
		String result = restTemplate.postForObject(url, req, String.class);
		System.out.println("****The result:" + result);
		
		return result;
	}

	/**
	 * @title main
	 * @description: 
	 * @param args
	 * @throws Exception 
	 * @date 2014-9-17 上午10:34:53
	 */
	public static void main(String[] args) throws Exception {
		RestTemplateTest rest = new RestTemplateTest();
		//rest.postJSON();
		//rest.postString();
		//rest.postList();
		//rest.post1();
//		String []ids = {"12347f8c-3312-11e4-9dbe-f31eac059c18", "45620e0b-3e37-11e4-a7dd-af51656ee167", "111"};
//		for(String taskId : ids){
//			String req = "<ingestTasksStatus ProgramID=\"" + taskId + "\" ProgramName=\"7\" Status=\"1\" Desc=\"SUCCESS\" />";
//			rest.postIngest(req);
//		}
		
		for(int i = 0; i < 100; i++){
			String req = "<ingestTasksStatus ProgramID=\"11\" ProgramName=\"7\" Status=\"1\" Desc=\"SUCCESS\" />";
			rest.postIngest(req);
		}
		
//		String req1 = "<ingestTasksStatus ProgramID=\"0644344e-3fde-11e4-865f-9f639f6ad43f\" ProgramName=\"7\" Status=\"0\" Desc=\"SUCCESS\" />";
//		rest.postIngest(req1);
		
		
		
	}

//	
//	public static void main(String[] args) {
//		RestTemplate restTemplate = new RestTemplate();
//
//		// get方式***********************************************************************************************************
//		// //参数直接放在URL中
//		// String message =
//		// restTemplate.getForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijie&id=80",
//		// String.class );
//		//	
//		// //参数使用MAP传递
//		// Map<String ,Object> urlVariables = new HashMap<String ,Object>();
//		// urlVariables.put("name", "zhaoshijie");
//		// urlVariables.put("id", 80);
//		// String message2 =
//		// restTemplate.getForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate",
//		// String.class, urlVariables);
//
//		// delete方式***********************************************************************************************************
//		// delete方法（注意：delete方法没有返回值，说明，id=0这个参数在服务器端可以不定义该参数，直接使用request获取）
//		// restTemplate.delete("http://localhost:8080/yongbarservice/appstore/appgoods/deleteranking?id=0");
//
//		// post方式***********************************************************************************************************
//		// 使用MAP传递参数
//		// Map<String ,Object> urlVariables = new HashMap<String ,Object>();
//		// urlVariables.put("name", "zhaoshijie");
//		// urlVariables.put("id", 80);
//		// String message3 =
//		// restTemplate.postForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate",null,
//		// String.class, urlVariables);
// 		//restTemplate.postForObject(url, request, responseType, uriVariables)
//
//		// 直接使用URL传递参数
//		// String message =
//		// restTemplate.postForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijie&id=80",null,
//		// String.class );
//
//		// put方式***********************************************************************************************************
//		// 注意：delete方法没有返回值，说明，id=0这个参数在服务器端可以不定义该参数，直接使用request获取
//		restTemplate.put("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijie&id=80",
//						null);
//
//		// System.out.println(message);
//		// System.out.println(message2);
//		// System.out.println(message3);
//	}
}